// pages/personal/personal.js
import request from '../../utils/request'
let startY = 0 // 手指起始坐标
let moveY = 0 // 手指移动坐标
let moveDistance = 0 // 手指移动距离
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: 'translateY(0)',
    coverTransition: '',
    userInfo: {},
    recentPlayList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getStorage({
      key: 'userInfo',
      success: res => {
        console.log('uesrInfo---', res);
        console.log('uesrInfo---', JSON.parse(res.data));
        this.setData({
          userInfo: JSON.parse(res.data)
        }, async () => {
          this.getUserRecentPlay()
        })
      }
    })
  },
  async getUserRecentPlay() {
    const recnetPlayRes = await request('/user/record', { uid: this.data.userInfo.userId, type: 1 })
    console.log('recnetPlayRes---', recnetPlayRes);
    if (recnetPlayRes.code == 200) {
      let index = 0
      let data = recnetPlayRes.weekData.slice(0, 10).map(item => {
        item.id = index++
        return item
      })
      this.setData({
        recentPlayList: data
      })
    }
  },

  touchStart(e) {
    console.log(e);
    startY = e.touches[0].clientY
  },
  touchMove(e) {
    moveY = e.touches[0].clientY
    moveDistance = moveY - startY
    if (moveDistance <= 0) return
    if (moveDistance >= 80) moveDistance = 80
    this.setData({
      coverTransition: '',
      coverTransform: `translateY(${moveDistance}rpx)`
    })
  },
  touchEnd() {
    this.setData({
      coverTransition: 'transform 0.5s',
      coverTransform: 'translateY(0)'
    })
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})